package com.sservice.web.controller.admin;

import com.sservice.common.SameUrlData;
import com.sservice.model.entity.Goods;
import com.sservice.service.service.ExportService;
import com.sservice.service.service.IGoodsService;
import com.sservice.service.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by suweiming on 2017/12/20.
 */

@Controller
@RequestMapping("/admin/exports")
public class ExportsController {

    @Autowired
    private IGoodsService depService;
    @Autowired
    private ExportService exportService;

    @Autowired
    private ISysLogService sysLogService;


    @SameUrlData
    @RequestMapping(value = "/goods", method = RequestMethod.GET)
    public OutputStream goods(Goods arg, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/binary;charset=GBK");

        String fileName = new String(("产品Excel").getBytes(), Charset.defaultCharset());
        response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式
        String hql = "产品"; // sheet 名称
        List<Map> lm = new ArrayList<Map>();
        for (int i = 0; i < 10; i++) {
            Map map = new HashMap();
            map.put("商品名", "甲苯" + i);
            map.put("商品单价", "商品单价" + i);
            map.put("商品单位", "商品单位" + i);
            lm.add(map);
        }
        return exportService.exportExcel(hql, lm, outputStream);
    }

}
